#!/bin/bash

# MySQL 用户名
USER="root"

# MySQL 密码
PASSWORD="root"

# MySQL 主机
HOST="1.94.42.202"

# MySQL 端口
PORT="33306"

# 导出路径
BACKUP_DIR="/home/software/mysql/backup/"

# 获取当前日期
DATE=$(date +"%Y%m%d%H%M")

# 创建备份目录
mkdir -p "$BACKUP_DIR"

# 获取所有数据库列表
DATABASES=$(mysql -u$USER -p$PASSWORD -h$HOST -P$PORT -e "SHOW DATABASES;" | grep -Ev "(Database|information_schema|performance_schema|mysql|sys|dbkuhou-test|scm_prod|ry-config)")

# 导出每个数据库
for DB in $DATABASES; do
    SQL_FILE="$BACKUP_DIR/${DB}_$DATE.sql"
    #mysqldump --column-statistics=0 -u$USER -p$PASSWORD -h$HOST -P$PORT --routines --triggers --events --single-transaction --quick --lock-tables=false "$DB" > "$SQL_FILE"
    mysqldump  -u$USER -p$PASSWORD -h$HOST -P$PORT --routines --triggers --events --single-transaction --quick --lock-tables=false "$DB" > "$SQL_FILE"
    echo "Database $DB has been exported to $SQL_FILE"
done

echo "All databases have been exported."